package www.openteam.com.model.business;

import android.graphics.Bitmap;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.DlryNtPrimaryTable;
import www.openteam.com.bean.DlryNtSublist;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.ShowSignature;

/**
 * Created by Administrator on 2019-07-30.
 */

public class DlryNoteModel extends BaseModel {

    public void getPrimaryTable(String code, final PrimaryTableListener primaryTableListener) {
//        Observable.
        httpApiService.findPrimaryTable(code).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DlryNtPrimaryTable>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        primaryTableListener.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(DlryNtPrimaryTable dlryNtPrimaryTable) {
                        primaryTableListener.success(dlryNtPrimaryTable);
                    }
                });

    }

    public void getSublist(String code, final SublistListener sublistListener) {
        httpApiService.findSubList(code).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DlryNtSublist>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        sublistListener.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(DlryNtSublist dlryNtSublist) {
                        sublistListener.success(dlryNtSublist);
                    }
                });

    }

    public void postSignature(File file, String zbid, String returnMan, String returnDate, String returnStatus,
                              final SignatureListener signatureListener) {

        Map<String, RequestBody> map = new HashMap<>();
        if (null == file) {
            map.put("signature\"; filename=\"", null);
        } else {
            //"image/png" 是内容类型，后台设置的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
         /*
          * 这里重点注意：
          * com_img[]里面的值为服务器端需要key   只有这个key 才可以得到对应的文件
          * filename是文件的名字，包含后缀名的   比如:abc.png
          */
            map.put("signature\"; filename=\"" + file.getName(), requestBody);
        }
        httpApiService.postSignture(map, zbid, returnMan, returnDate, returnStatus).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<InsertStatus>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        signatureListener.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(InsertStatus insertStatus) {
                        signatureListener.success(insertStatus);
                    }
                });

    }

    public void cancleRecipe(String zbid, final CancleRcpListener cancleRcpListener) {
        httpApiService.cancleRecipe(zbid).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<InsertStatus>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        cancleRcpListener.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(InsertStatus insertStatus) {
                        cancleRcpListener.success(insertStatus);
                    }
                });

    }

    public void showSignature(String zbid, final ShowSignatureListener showSignatureListener) {
        httpApiService.shwoSignature(zbid).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ShowSignature>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        showSignatureListener.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(ShowSignature showSignature) {
                        showSignatureListener.success(showSignature);
                    }
                });

    }

    public interface CancleRcpListener {
        void success(InsertStatus insertStatus);

        void fail(String msg);
    }

    public interface PrimaryTableListener {
        void success(DlryNtPrimaryTable dlryNtPrimaryTable);

        void fail(String msg);
    }

    public interface SublistListener {
        void success(DlryNtSublist dlryNtSublist);

        void fail(String msg);
    }

    public interface SignatureListener {
        void success(InsertStatus insertStatus);

        void fail(String msg);
    }

    public interface ShowSignatureListener {
        void success(ShowSignature showSignature);

        void fail(String msg);
    }
}
